package com.ykl.base.restful.validator;


import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class OptionValsValidator implements ConstraintValidator<OptionVals, Object> {

    private String[] options;

    private boolean nullable;

    public void initialize(OptionVals ann) {
        options = ann.value();
        nullable = ann.nullable();
    }

    public boolean isValid(Object val, ConstraintValidatorContext constraintValidatorContext) {
        if (val == null&&nullable) {
            return true;
        }
        String str = String.valueOf(val);
        return StringUtils.containsAny(str, options);
    }
}
